/**
 * @file adapter_data.h
 * @brief 数据适配层 协议说明 数据结构序列化
 * @copyright Copyright (c) 2020 YUSUR Technology Co., Ltd. All Rights Reserved. Learn more at www.yusur.tech.
 * @author liuzx@yusur.tech
 * @date 2023-06-29
 */

#ifndef _YUSUR_ADAPTER_DATA_H_
#define _YUSUR_ADAPTER_DATA_H_

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <string>
#include <vector>

#pragma pack(push, 1)

/**
 * @brief 逐笔数据中成交量
 */
struct IndexVolume
{
    uint32_t stock_id;  // 股票对应的id
    uint32_t volume;    // 成交量
    uint32_t index;     // 内部使用,使用者不理会
    IndexVolume() = default;
    IndexVolume(uint32_t _index) : index(_index), volume(0) {}
    IndexVolume(uint32_t _index, uint32_t _id) : index(_index), stock_id(_id), volume(0) {}
    IndexVolume(uint32_t _index, uint32_t _id, uint32_t _volume) : index(_index), stock_id(_id), volume(_volume) {}
    template <typename Ar>
    void serialize(Ar& ar, const unsigned int version)
    {
        ar& volume& stock_id;
    }
};

/**
 * @brief 风控模块所需逐笔行情数据,每秒记录行情
 */
#define MSG_YUSUR_Detail 1000
struct YusurHqDetail
{
    uint64_t                 timestamp_id;
    std::vector<IndexVolume> index_volume_vec;
    template <typename Ar>
    void serialize(Ar& ar, const unsigned int version)
    {
        ar& timestamp_id& index_volume_vec;
    }
};

/**
 * @brief 风控模块所需分片行情数据
 */
#define MSG_YUSUR_HQDATA 1001
struct YusurHqdata
{
    uint32_t              stock_id;    // 股票代码
    uint32_t              last_price;  // 最新价
    uint32_t              open_price;
    uint32_t              high_price;
    uint32_t              low_price;
    uint32_t              pre_price;  // 昨收价
    uint64_t              timestamp_id;
    std::vector<uint32_t> BuyPriceQe;
    std::vector<uint32_t> BuyOrderQtyQe;
    std::vector<uint32_t> SellPriceQe;
    std::vector<uint32_t> SellOrderQtyQe;
    template <typename Ar>
    void serialize(Ar& ar, const unsigned int version)
    {
        ar& stock_id;
        ar& last_price;
        ar& pre_price;
        ar& timestamp_id;
        ar& BuyPriceQe;
        ar& BuyOrderQtyQe;
        ar& SellPriceQe;
        ar& SellOrderQtyQe;
    }
};

/**
 * @brief 委托请求数据
 */
#define MSG_YUSUR_ORDERCHECKREQ 1002

struct YusurOrderCheckRequest
{
    uint32_t MsgType;           // 值为1
    uint32_t MsgLen;
    uint32_t EntrustDate;       // 委托日期
	uint32_t BusinessTime;      // 委托时间 转换成60进制
    uint32_t ExternSysNo;       // 外部系统号
    uint32_t ExternReportNo;    // 外部申报号
    char     FundCode[33];      // 产品编号
    char     StockAccount[21];  // 证券账户
    uint8_t  ExchangeType;      // 交易市场
    char     SecurityCode[16];  // 证券代码
    uint16_t EntrustDirection;  // 委托方向
    uint32_t EntrustAmount;     // 委托数量
    uint8_t  PriceType;         // 价格类型
    uint64_t EntrustPrice;      // 委托价格
    uint64_t EntrustBalance;    // 委托金额
    uint32_t NodeId;            // 节点ID编号
    uint32_t FunctionID;        // 业务编号
    uint64_t Reserved;          // 保留字段
    template <typename Ar>
    void serialize(Ar& ar, const unsigned int version)
    {
        ar& MsgType& MsgLen& EntrustDate& ExternSysNo& ExternReportNo& FundCode& StockAccount& ExchangeType&
            SecurityCode& EntrustDirection& EntrustAmount& PriceType& EntrustPrice& EntrustBalance& NodeId& FunctionID&
                Reserved;
    }
};

/**
 * @brief 委托请求响应 由风控服务发给适配层
 */
#define MSG_YUSUR_ORDERCHECKRSP 1003

struct YusurOrderCheckResponse
{
    uint32_t MsgType;         // 值为10001
    uint32_t MsgLen;
    uint32_t ExternSysNo;     // 外部系统号
    uint32_t ExternReportNo;  // 外部申报号
    uint32_t RiskCheckNo;     // 风控检查流水号
    uint32_t ResultCode;      // 风控检查结果
    char     ErrorInfo[128];  // 错误信息
    uint32_t NodeId;          // 节点ID编号
    uint32_t FunctionID;      // 业务编号
    uint64_t Reserved;        // 保留字段
    template <typename Ar>
    void serialize(Ar& ar, const unsigned int version)
    {
        ar& MsgType;
        ar& MsgLen;
        ar& ExternSysNo;
        ar& ExternReportNo;
        ar& RiskCheckNo;
        ar& ResultCode;
        ar& ErrorInfo;
        ar& NodeId;
        ar& FunctionID;
        ar& Reserved;
    }
};

/**
 * @brief 委托确认响应
 */
#define MSG_YUSUR_ORDERCOMFIRMRSP 1004
struct YusurOrderConfirmRsp
{
    uint32_t MsgType;           // 值为10011
    uint32_t MsgLen;
    uint32_t ExternSysNo;       // 外部系统号
    uint32_t ExternReportNo;    // 外部申报号
    uint32_t Result;            // 委托结果
    uint32_t OriginalOrderNum;  // 原始报单数量
    uint32_t CancelOrderNum;    // 撤单数量
    uint32_t NodeId;            // 节点ID编号
    uint32_t ExchangeOrderId;   // 交易所订单编号
    uint32_t FunctionID;        // 业务编号
    uint64_t Reserved;          // 保留字段
    template <typename Ar>
    void serialize(Ar& ar, const unsigned int version)
    {
        ar& MsgType& MsgLen& ExternSysNo& ExternReportNo& Result& OriginalOrderNum& CancelOrderNum& NodeId&
            ExchangeOrderId& FunctionID& Reserved;
    }
};

/**
 * @brief 撤单确认响应
 */
#define MSG_YUSUR_ORDERCANCELRSP 1005
struct YusurOrderCancelRsp
{
    uint32_t MsgType;                 // 值为10012
    uint32_t MsgLen;
    uint32_t ExternSysNo;             // 外部系统号
    uint32_t ExternReportNo;          // 外部申报号
    uint32_t OriginalExternSysNo;     // 原订单外部系统号
    uint32_t OriginalExternReportNo;  // 原订单外部申报号
    uint32_t Result;                  // 委托结果
    uint32_t OriginalOrderNum;        // 原始报单数量 //未填写
    uint32_t CancelOrderNum;          // 撤单数量
    uint32_t NodeId;                  // 节点ID编号
    uint32_t ExchangeOrderId;         // 交易所订单编号 //未填写
    uint32_t FunctionID;              // 业务编号（FunctionID）
    uint64_t Reserved;                // 保留字段
    template <typename Ar>
    void serialize(Ar& ar, const unsigned int version)
    {
        ar& MsgType& MsgLen& ExternSysNo& ExternReportNo& OriginalExternSysNo& OriginalExternReportNo& Result&
            OriginalOrderNum& CancelOrderNum& NodeId& ExchangeOrderId& FunctionID& Reserved;
    }
};

/**
 * @brief 成交回报
 */
#define MSG_YUSUR_ORDERDEALINFO 1006

struct YusurOrderDealInfo
{
    uint32_t MsgType;           // 值为10021
    uint32_t MsgLen;
    uint32_t ExternSysNo;       // 外部系统号
    uint32_t ExternReportNo;    // 外部申报号
    uint32_t NodeId;            // 节点 ID 编号
    uint32_t DealNo;            // 成交编号
    char     StockAccount[21];  // 证券账户 //new do
    char     SecurityCode[16];  // 证券代码
    uint16_t EntrustDirection;  // 委托方向 //new do
    uint64_t BusinessPrice;     // 成交价格
    uint32_t BusinessAmount;    // 成交数量
    uint32_t ExternDealNo;      // 外部成交编号
    uint32_t BusinessTime;      // 成交时间
    uint32_t FunctionID;        // 业务编号（FunctionID）
    uint64_t Reserved;          // 保留字段
    template <typename Ar>
    void serialize(Ar& ar, const unsigned int version)
    {
        ar& MsgType& MsgLen& ExternSysNo& ExternReportNo& NodeId& DealNo& SecurityCode& BusinessPrice& BusinessAmount&
            ExternDealNo& BusinessTime& FunctionID& Reserved;
    }
};

/**
 * @brief 股票信息
 */
struct RuleStockInfo
{
    std::string security_exchange;  // 市场编号
    std::string security_id;        // 证券代码
    std::string security_name;      // 名称
    std::string security_type;      // 证券类型
    std::string security_sub_type;  // 证券子类
    double      prev_close_px;      // 昨收价格
    double      high_limit_px;      // 涨停价格
    double      low_limit_px;       // 跌停价格
    template <typename Ar>
    void serialize(Ar& ar, const unsigned int version)
    {
        ar& security_exchange;
        ar& security_id;
        ar& prev_close_px;
        ar& high_limit_px;
        ar& low_limit_px;
    }
};

/**
 * @brief 静态基本规则信息
 */
#define MSG_YUSUR_RISKCOPRULES 1007
struct YusurRiskCopRules
{
    int         riskcop_id;       // 规则ID
    int         system_id;        // 系统Id
    int         riskcon_type;     // 风控类型
    std::string riskcon_caption;  // 风控规则说明
    union Threshold
    {
        struct LTDY
        {
            int      rule_time_window;      // 规则时间窗，单位为秒
            uint32_t warn_v_threshold;      // 量预警阈值
            uint64_t warn_a_threshold;      // 金额预警阈值
            uint32_t warn_rate_threshold;   // 成交量占比预警阈值
            uint32_t warn_pcgh2_threshold;  // 价格偏离度预警阈
            uint32_t forbid_v_threshold;    // 量禁止阈值
            uint64_t forbid_a_threshold;
            uint32_t forbid_rate_threshold;
            uint32_t forbid_pcgh2_threshold;
        } ltdy;
        struct XJSB
        {
            int      rule_time_window;
            uint32_t entrust_v_threshold;              // 委托数量预警阈值
            uint64_t entrust_a_threshold;              // 委托金额预警阈值
            uint32_t entrust_v_rate_threshold;         // 委托量占比预警阈值
            uint32_t entrust_a_rate_threshold;         // 委托额占比预警阈值
            uint32_t entrust_count_threshold;          // 委托预警次数阈值
            uint32_t entrust_cancel_v_rate_threshold;  // 委托取消率阈值
        } xjsb;
    } threshold;
    uint64_t                   start_time;       // 规则生效时间 60进制
    uint64_t                   stop_time;        // 失效时间时间
    uint64_t                   noon_start_time;  // 上午收盘时间
    uint64_t                   noon_stop_time;   // 下午开盘时间
    std::vector<RuleStockInfo> stock_ids;
    std::vector<std::string>   account_ids;
    template <typename Ar>
    void serialize(Ar& ar, const unsigned int version)
    {
        ar& riskcop_id;
        ar& system_id;
        ar& riskcon_type;
        ar& riskcon_caption;
        if (riskcon_type == 1101)
        {
            ar& threshold.ltdy.rule_time_window;
            ar& threshold.ltdy.warn_v_threshold;
            ar& threshold.ltdy.warn_a_threshold;
            ar& threshold.ltdy.warn_rate_threshold;
            ar& threshold.ltdy.warn_pcgh2_threshold;
            ar& threshold.ltdy.forbid_v_threshold;
            ar& threshold.ltdy.forbid_a_threshold;
            ar& threshold.ltdy.forbid_rate_threshold;
            ar& threshold.ltdy.forbid_pcgh2_threshold;
        }
        else if (riskcon_type == 1102 || riskcon_type == 1103)
        {
            ar& threshold.xjsb.rule_time_window;
            ar& threshold.xjsb.entrust_v_threshold;
            ar& threshold.xjsb.entrust_a_threshold;
            ar& threshold.xjsb.entrust_v_rate_threshold;
            ar& threshold.xjsb.entrust_a_rate_threshold;
            ar& threshold.xjsb.entrust_count_threshold;
            ar& threshold.xjsb.entrust_cancel_v_rate_threshold;
        }
        ar& start_time;
        ar& stop_time;
        ar& noon_start_time;
        ar& noon_stop_time;
        ar& stock_ids;
        ar& account_ids;
    }
};

#pragma pack(pop)

#endif